package com.zzl.alligator.management.system.service.impl;


import cn.hutool.core.collection.CollectionUtil;
import com.zzl.alligator.common.constant.SystemConstants;
import com.zzl.alligator.management.system.domain.SysNotice;
import com.zzl.alligator.management.system.domain.SysUser;
import com.zzl.alligator.management.system.domain.SysUserNotice;
import com.zzl.alligator.management.system.mapper.SysNoticeMapper;
import com.zzl.alligator.management.system.mapper.SysUserAssistMapper;
import com.zzl.alligator.management.system.mapper.SysUserMapper;
import com.zzl.alligator.management.system.mapper.SysUserNoticeMapper;
import com.zzl.alligator.management.system.service.ISysNoticeService;
import com.zzl.alligator.management.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 通知公告表(SysNotice)服务层
 *
 * @author zhouziliang
 * @since 2020-06-30
 */
@Service
public class SysNoticeService implements ISysNoticeService {
    
    @Autowired
    private SysNoticeMapper sysNoticeMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysUserNoticeMapper sysUserNoticeMapper;
    @Autowired
    private SysUserAssistMapper sysUserAssistMapper;
    @Autowired
    private ISysUserService userService;
    
    /**
     * 通过ID查询单条数据
     *
     * @param noticeId 主键
     * @return 实例对象
     */
    @Override
    public SysNotice findSysNoticeById(Integer noticeId) {
        SysNotice notice = sysNoticeMapper.findSysNoticeById(noticeId);
        if(SystemConstants.NOTICE_TYPE_TZ.equals(notice.getNoticeType())) {
            List<SysUserNotice> list = sysUserNoticeMapper.findSysUserNoticeByNoticeId(noticeId);
            if (CollectionUtil.isNotEmpty(list)) {
                List<Long> userIds = list.stream().map(SysUserNotice::getUserId).collect(Collectors.toList());
                List<Long> deptIds = userService.getDeptIdsByUserIds(userIds);
                notice.setDeptIds(deptIds);
                notice.setUserIds(userIds);

            }
        }
        return notice;

    }



   /**
     * 通过实体作为筛选条件查询
     *
     * @param sysNotice 实例对象
     * @return 对象列表
     */
    @Override
    public List<SysNotice> findSysNoticeList(SysNotice sysNotice) {
        return sysNoticeMapper.findSysNoticeList(sysNotice);
    }

    /**
     * 新增数据
     *
     * @param sysNotice 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertSysNotice(SysNotice sysNotice) {
        int res = sysNoticeMapper.insertSysNotice(sysNotice);
        if(SystemConstants.NOTICE_TYPE_GG.equals(sysNotice.getNoticeType())) {
            sysUserAssistMapper.userPubNoticePlus();
        }
        if(SystemConstants.NOTICE_TYPE_TZ.equals(sysNotice.getNoticeType())) {
            insertUserNotice(sysNotice);
        }
        return res;
    }

    private void insertUserNotice(SysNotice sysNotice) {
        List<Long> deptIds = sysNotice.getDeptIds();
        List<Long> userIds = sysNotice.getUserIds();
        List<SysUser> list = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(deptIds)) {
            deptIds.forEach(deptId -> {
                List<SysUser> userList = sysUserMapper.selectUserList(new SysUser() {{
                    setDeptId(deptId);
                }});
                list.addAll(userList);
            });
        }
        List<Long> userIdList = list.stream().map(SysUser::getUserId).collect(Collectors.toList());
        if (userIds == null) {
            userIds = new ArrayList<>();
        }
        userIds.addAll(userIdList);
        int noticeId = sysNotice.getNoticeId();
        List<Long> uIdList = userIds.stream().distinct().collect(Collectors.toList());
        List<SysUserNotice> userNotices = uIdList.stream().map(userId ->
                new SysUserNotice(userId, noticeId, SystemConstants.STATUS_NOT_READ, null)
        ).collect(Collectors.toList());
        sysUserAssistMapper.batchUserPersonNoticePlus(uIdList);
        sysUserNoticeMapper.batchInsert(userNotices);
    }

    /**
     * 修改数据
     *
     * @param sysNotice 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateSysNotice(SysNotice sysNotice) {
        Integer noticeId = sysNotice.getNoticeId();
        SysNotice notice = sysNoticeMapper.findSysNoticeById(sysNotice.getNoticeId());
        if (SystemConstants.NOTICE_TYPE_TZ.equals(notice.getNoticeType())) {
            List<SysUserNotice> userNotices = sysUserNoticeMapper.findSysUserNoticeByNoticeId(noticeId);
            if(CollectionUtil.isNotEmpty(userNotices)) {
                List<Long> oldUserIds = userNotices.stream().map(SysUserNotice::getUserId).collect(Collectors.toList());
                sysUserAssistMapper.userPersonNoticeBatchSub(oldUserIds);
            }
        }
        int res = sysNoticeMapper.updateSysNotice(sysNotice);
        sysUserNoticeMapper.deleteByNoticeId(noticeId);
        if(SystemConstants.NOTICE_TYPE_TZ.equals(sysNotice.getNoticeType())) {
            insertUserNotice(sysNotice);
        }
        return res;
    }

    /**
     * 通过主键删除数据
     *
     * @param noticeId 主键
     * @return 是否成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteById(Integer noticeId) {
        sysUserNoticeMapper.deleteByNoticeId(noticeId);
        return sysNoticeMapper.deleteById(noticeId);
    }

    /**
     * 根据用户Id获取通知列表
     *
     * @param id 用户ID
     * @return 通知列表
     */
    @Override
    public List<SysNotice> findSysNoticeListByUserId(Long id) {
        return sysNoticeMapper.findSysNoticeListByUserId(id);
    }

    /**
     * 获取公告列表
     *
     * @return 公告列表
     */
    @Override
    public List<SysNotice> findPublicNotice() {
        return sysNoticeMapper.selectPublicNotice();
    }
}